/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service.alarm;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.dao.alarm.EleSourceAlarmDao;
import com.thinkgem.jeesite.modules.source.dao.alarm.EleSourceAlarmGroupDao;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarm;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarmGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 警告资源管理Service
 * @author ZC
 * @version 2017-04-23
 */
@Service
@Transactional(readOnly = true)
public class EleSourceAlarmGroupService extends CrudService<EleSourceAlarmGroupDao, EleSourceAlarmGroup> {

	@Autowired
	private EleSourceAlarmDao eleSourceAlarmDao;
	
	public EleSourceAlarmGroup get(String id) {
		EleSourceAlarmGroup eleSourceAlarmGroup = super.get(id);
		if(null != eleSourceAlarmGroup){
			eleSourceAlarmGroup.setEleSourceAlarmList(eleSourceAlarmDao.findList(new EleSourceAlarm(eleSourceAlarmGroup)));
		}
		return eleSourceAlarmGroup;
	}

	public List<EleSourceAlarm> findAlarmsByGroupId(String groupId) {
		EleSourceAlarmGroup eleSourceAlarmGroup = new EleSourceAlarmGroup(groupId);
		return eleSourceAlarmDao.findList(new EleSourceAlarm(eleSourceAlarmGroup));
	}


	public List<EleSourceAlarmGroup> findList(EleSourceAlarmGroup eleSourceAlarmGroup) {
		return super.findList(eleSourceAlarmGroup);
	}
	
	public Page<EleSourceAlarmGroup> findPage(Page<EleSourceAlarmGroup> page, EleSourceAlarmGroup eleSourceAlarmGroup) {
		return super.findPage(page, eleSourceAlarmGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceAlarmGroup eleSourceAlarmGroup) {
		super.save(eleSourceAlarmGroup);
		for (EleSourceAlarm eleSourceAlarm : eleSourceAlarmGroup.getEleSourceAlarmList()){
			if (eleSourceAlarm.getId() == null){
				continue;
			}
			if (EleSourceAlarm.DEL_FLAG_NORMAL.equals(eleSourceAlarm.getDelFlag())){
				if (StringUtils.isBlank(eleSourceAlarm.getId())){
					eleSourceAlarm.setGroup(eleSourceAlarmGroup);
					eleSourceAlarm.preInsert();
					eleSourceAlarmDao.insert(eleSourceAlarm);
				}else{
					eleSourceAlarm.preUpdate();
					eleSourceAlarmDao.update(eleSourceAlarm);
				}
			}else{
				eleSourceAlarmDao.delete(eleSourceAlarm);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceAlarmGroup eleSourceAlarmGroup) {
		super.delete(eleSourceAlarmGroup);
		eleSourceAlarmDao.delete(new EleSourceAlarm(eleSourceAlarmGroup));
	}
	
}